package com.companyName.sample.controller.controller.sys;

import com.companyName.sample.api.service.sys.IUserInfoService;
import com.companyName.sample.bean.dto.sys.UserInfoDto;
import com.companyName.sample.common.bean.ResponseData;
import com.companyName.sample.controller.controller.BaseController;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class UserInfoController extends BaseController {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(UserInfoController.class);

    @Autowired
    IUserInfoService userInfoServiceImpl;

    /**
     * 查询 ID
     */
    @RequestMapping(value = "/user/getuser/{userid}", method = RequestMethod.GET)
    public ModelAndView getUserInfo(@PathVariable(value = "userid") String userID) {
        ModelAndView model = new ModelAndView();
        ResponseData<UserInfoDto> responseData = userInfoServiceImpl.getUserInfo(new Integer(userID));
        model.addObject(responseData);
        model.setViewName("userInfo");
        return model;
    }

    /**
     * 查询 List
     */
    @RequestMapping(value = "/user/getuserlist", method = RequestMethod.GET)
    public ModelAndView getUserInfoList(@RequestParam(value = "username", required = false) String userName,
                                        @RequestParam(value = "page_id", defaultValue = "0") Integer pageId,
                                        @RequestParam(value = "page_size", defaultValue = "20") Integer pageSize) {
        ModelAndView model = new ModelAndView();
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setUsername(userName);
        ResponseData<List<UserInfoDto>> responseData = userInfoServiceImpl.getUserInfoList(userInfoDto, pageId, pageSize);
        model.addObject(responseData.getResult());
        model.setViewName("userInfo");
        return model;
    }

    @InitBinder("userInfoDto")
    public void initBinder1(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("userInfoDto.");
    }

    /**
     * 查询 List
     */
    @RequestMapping(value = "/user/getuserlist1", method = RequestMethod.GET)
    public ModelAndView getUserInfoList1(@ModelAttribute(value = "userInfoDto") UserInfoDto userInfoDto,
                                        @RequestParam(value = "page_id", defaultValue = "0") Integer pageId,
                                        @RequestParam(value = "page_size", defaultValue = "20") Integer pageSize) {
        ModelAndView model = new ModelAndView();
        userInfoDto.setPassword("a");
        ResponseData validate = validateResponse(userInfoDto);
        if(validate != null){
            return wrapModelAndFailure(model,validate);
        }

        ResponseData<List<UserInfoDto>> responseData = userInfoServiceImpl.getUserInfoList(userInfoDto, pageId, pageSize);
        model.addObject(responseData.getResult());
        model.setViewName("userInfo");
        return model;
    }

    /*
     * 增加
     */
    @RequestMapping(value = "/user/putuser", method = RequestMethod.PUT)
    public ResponseEntity<?> putUserInfo(HttpServletRequest httpRequest,
                                         @ModelAttribute UserInfoDto userInfoDto) {
        ResponseData<Integer> responseData = userInfoServiceImpl.putUserInfo(userInfoDto);
        return successResponse(responseData.getResult());
    }

    /*
     * 修改
     */
    @RequestMapping(value = "/user/postuser", method = RequestMethod.POST)
    public ResponseEntity<?> postUserInfo(HttpServletRequest httpRequest,
                                          @ModelAttribute UserInfoDto userInfoDto) {
        ResponseData<Integer> responseData = userInfoServiceImpl.postUserInfo(userInfoDto);
        return successResponse(responseData.getResult());
    }

    /*
     * 删除
     */
    @RequestMapping(value = "/user/deleteuser", method = RequestMethod.DELETE)
    public ResponseEntity<?> deleteUserInfo(HttpServletRequest httpRequest,
                                            @RequestParam(value = "id", required = false) String userID) {
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setId(new Integer(userID));
        ResponseData<Integer> responseData = userInfoServiceImpl.deleteUserInfo(userInfoDto);
        return successResponse();
    }

    /*
     * 增加，测试事务
     */
    @RequestMapping(value = "/user/putuserT", method = RequestMethod.PUT)
    public ResponseEntity<?> putUserInfoT(HttpServletRequest httpRequest, HttpServletResponse httpReponse,
                                          @ModelAttribute UserInfoDto userInfoDto) {

        ResponseData<Integer> responseData;
        if ("T".equals(userInfoDto.getPassword())) {
            responseData = userInfoServiceImpl.putUserInfoT(userInfoDto);
        } else {
            responseData = userInfoServiceImpl.insertUserInfoT(userInfoDto);
        }
        return successResponse(responseData.getResult());
    }

}
